package com.lifeng.listener;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Map;


/**
 * @description:
 * @author: lifeng
 * @create: 2020/9/18 16:08
 * 获取msgid msgkey等信息
 */
@Component
@RocketMQMessageListener(topic = "lifeng-topic-4", consumerGroup = "lifeng-topic-consumer-group-4")
public class ListenerService4 implements RocketMQListener<MessageExt> {
    @Override
    public void onMessage(MessageExt message) {
        String msgId = message.getMsgId();
        String msgKey = message.getKeys();
        String tags = message.getTags();
        String topic = message.getTopic();
        Map<String,String> prop = message.getProperties();
        try {
            System.out.println(new String(message.getBody(),"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("msgid:"+msgId+",keys:"+msgKey+",tags:"+tags+",topic:"+topic+",props:"+prop);
        System.out.println(message);
        /**
         * 手动控制消费重试次数
         * DefaultMQPushConsumer类本来提供了消费重试次数maxReconsumeTimes ,
         * 但是boot-start封装后并没有提供配置的参数，只能手动控制
         * */
        /*if(message.getReconsumeTimes() > 3){
            return;
        }else{
            new RuntimeException("消费异常,重试");
        }*/
    }
}
